package appUtils;

import android.app.Activity;
import android.app.Dialog;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.jhlibrarys.R;


/**
 * Created by lj on 2017/5/20 0020.
 * update by Joe
 */

public class CustomSingleInputDialog {
    private final Activity mActivity;
    private onNoOnclickListener mOnNoOnclickListener;//Cancel button was clicked the listener
    private onYesOnclickListener mOnYesOnclickListener;//Determine the button was clicked the listener
    private ViewHolder holder;
    private Dialog dialogWindow;

    public CustomSingleInputDialog(Activity activity) {
        this.mActivity = activity;
        createDialog();
    }

    public CustomSingleInputDialog(Activity activity, onYesOnclickListener yesOnclickListener) {
        this.mActivity = activity;
        this.mOnYesOnclickListener = yesOnclickListener;
        createDialog();
    }

    public CustomSingleInputDialog(Activity activity, onNoOnclickListener noOnclickListener,
                                   onYesOnclickListener yesOnclickListener) {
        this.mActivity = activity;
        this.mOnNoOnclickListener = noOnclickListener;
        this.mOnYesOnclickListener = yesOnclickListener;
        createDialog();
    }

    private void createDialog() {
        dialogWindow = new Dialog(mActivity, R.style.CusDialog);
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View dialogWindowView = inflater.inflate(R.layout.dialog_single_input, null);
        dialogWindow.setContentView(dialogWindowView);
        try {
            WindowManager.LayoutParams attributes = dialogWindow.getWindow().getAttributes();
            DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics();
            attributes.width = (int) (metrics.widthPixels * 0.9);
            attributes.height = (int) (metrics.heightPixels * 0.9);
            dialogWindow.getWindow().setAttributes(attributes);
        } catch (Exception e) {
            e.printStackTrace();
            PayTokenException.getInstance().saveExceptionFile("" +
                    "CustomSingleInputDialog getAttributes error: " + e.getMessage());

        }
//        dialogWindow.setCancelable(true);//屏蔽物理返回键
        holder = new ViewHolder(dialogWindowView);
    }

    public void showCustomDialog() {
        if (dialogWindow != null && !dialogWindow.isShowing()) {
            dialogWindow.show();
        }
        holder.etInputDialogMessage.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                    mOnYesOnclickListener.onYesClick();
                }
                return false;
            }
        });
        holder.ibInputDialogVisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.etInputDialogMessage.getInputType() != InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
                    holder.etInputDialogMessage.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    holder.ibInputDialogVisible.setImageResource(R.drawable.icon_pwd_visible);
                } else {
                    holder.etInputDialogMessage.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    holder.ibInputDialogVisible.setImageResource(R.drawable.icon_pwd_invisible);
                }
                holder.etInputDialogMessage.setSelection(holder.etInputDialogMessage.getText().toString().length());
            }
        });
        holder.ibtnInputDialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnNoOnclickListener != null) {
                    mOnNoOnclickListener.onNoClick();
                }
                dismiss();
            }
        });
        holder.btnInputDialogCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnYesOnclickListener != null) {
                    mOnYesOnclickListener.onYesClick();
                }
                dismiss();
            }
        });
    }


    public String getUserPwd() {
        if (holder != null) {
            String pwd = holder.etInputDialogMessage.getText().toString();
            holder.etInputDialogMessage.setText("");
            return pwd;
        }
        return "";
    }

    public void setOnNoOnclickListener(onNoOnclickListener listener) {
        this.mOnNoOnclickListener = listener;
    }

    public void setOnYesOnclickListener(onYesOnclickListener listener) {
        this.mOnYesOnclickListener = listener;
    }

    public void setTitleText(String content) {
        if (holder != null) {
            holder.tvInputDialogTitle.setText(content);
        }
    }

    public void setYesBtnText(String content) {
        if (holder != null) {
            holder.btnInputDialogCommit.setText(content);
        }
    }

    public void dismiss() {
        if (dialogWindow != null && dialogWindow.isShowing()) {
            dialogWindow.dismiss();
        }
    }

    /**
     * Set button and cancel by click on the interface
     */
    public interface onYesOnclickListener {
        void onYesClick();
    }

    public interface onNoOnclickListener {
        void onNoClick();
    }

    private static class ViewHolder {
        final TextView tvInputDialogTitle;
        final EditText etInputDialogMessage;
        final Button btnInputDialogCommit;
        final ImageButton ibtnInputDialogClose;
        final ImageButton ibInputDialogVisible;

        ViewHolder(View view) {
            tvInputDialogTitle = (TextView) view.findViewById(R.id.tv_input_dialog_title);
            etInputDialogMessage = (EditText) view.findViewById(R.id.et_input_dialog_message);
            btnInputDialogCommit = (Button) view.findViewById(R.id.btn_input_dialog_commit);
            ibtnInputDialogClose = (ImageButton) view.findViewById(R.id.ibtn_input_dialog_close);
            ibInputDialogVisible = (ImageButton) view.findViewById(R.id.ib_input_dialog_visible);
        }
    }
}

